Public Class frmMetMai

    Private Sub butCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butCancel.Click
        Me.Close()
    End Sub

    Private Sub frmMetMai_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim iControl As Control
        For Each iControl In Me.Controls
            If Microsoft.VisualBasic.Left(iControl.Tag, 1) = "F" Then
                Dim iText As TextBox
                iText = iControl
                iText.Text = "0"
                AddHandler iControl.KeyPress, AddressOf txtKeyPressEnter
            End If
        Next

        txtDai1.Text = frmMain.tblDetail(frmMain.grdDetail.CurrentRowIndex).Item("chieu_dai")
        txtDai2.Text = frmMain.tblDetail(frmMain.grdDetail.CurrentRowIndex).Item("chieu_dai")
        txtRong1.Text = frmMain.tblDetail(frmMain.grdDetail.CurrentRowIndex).Item("chieu_rong")
        txtRong2.Text = frmMain.tblDetail(frmMain.grdDetail.CurrentRowIndex).Item("chieu_rong")

        AddHandler txtDai1.KeyPress, AddressOf clsSupport.txtNumericKeyPress
        AddHandler txtDai2.KeyPress, AddressOf clsSupport.txtNumericKeyPress
        AddHandler txtRong1.KeyPress, AddressOf clsSupport.txtNumericKeyPress
        AddHandler txtRong2.KeyPress, AddressOf clsSupport.txtNumericKeyPress

        AddHandler txtDai1.Validated, AddressOf txtDai1_valid
        AddHandler txtDai2.Validated, AddressOf txtDai2_valid
        AddHandler txtRong1.Validated, AddressOf txtRong1_valid
        AddHandler txtRong2.Validated, AddressOf txtRong2_valid

    End Sub
    Public Sub txtKeyPressEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If AscW(e.KeyChar) = 13 Then
            SendKeys.Send("{Tab}")
        End If
    End Sub

#Region "Validation"
    Private Sub txtDai1_valid(ByVal sender As Object, ByVal e As System.EventArgs)
        If Trim(txtDai1.Text) = "" Then txtDai1.Text = "0"
        If Trim(txtDai2.Text) = "" Then txtDai2.Text = "0"
        If Trim(txtRong1.Text) = "" Then txtRong1.Text = "0"
        If Trim(txtRong2.Text) = "" Then txtRong2.Text = "0"
        txtTongmm.Text = CDbl(txtDai1.Text) + CDbl(txtDai2.Text) + CDbl(txtRong1.Text) + CDbl(txtRong2.Text)
    End Sub

    Private Sub txtDai2_valid(ByVal sender As Object, ByVal e As System.EventArgs)
        If Trim(txtDai1.Text) = "" Then txtDai1.Text = "0"
        If Trim(txtDai2.Text) = "" Then txtDai2.Text = "0"
        If Trim(txtRong1.Text) = "" Then txtRong1.Text = "0"
        If Trim(txtRong2.Text) = "" Then txtRong2.Text = "0"
        txtTongmm.Text = CDbl(txtDai1.Text) + CDbl(txtDai2.Text) + CDbl(txtRong1.Text) + CDbl(txtRong2.Text)
    End Sub

    Private Sub txtRong1_valid(ByVal sender As Object, ByVal e As System.EventArgs)
        If Trim(txtDai1.Text) = "" Then txtDai1.Text = "0"
        If Trim(txtDai2.Text) = "" Then txtDai2.Text = "0"
        If Trim(txtRong1.Text) = "" Then txtRong1.Text = "0"
        If Trim(txtRong2.Text) = "" Then txtRong2.Text = "0"
        txtTongmm.Text = CDbl(txtDai1.Text) + CDbl(txtDai2.Text) + CDbl(txtRong1.Text) + CDbl(txtRong2.Text)
    End Sub

    Private Sub txtRong2_valid(ByVal sender As Object, ByVal e As System.EventArgs)
        If Trim(txtDai1.Text) = "" Then txtDai1.Text = "0"
        If Trim(txtDai2.Text) = "" Then txtDai2.Text = "0"
        If Trim(txtRong1.Text) = "" Then txtRong1.Text = "0"
        If Trim(txtRong2.Text) = "" Then txtRong2.Text = "0"
        txtTongmm.Text = CDbl(txtDai1.Text) + CDbl(txtDai2.Text) + CDbl(txtRong1.Text) + CDbl(txtRong2.Text)
    End Sub
#End Region

    Private Sub butSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSave.Click
        Dim nSo_luong As Decimal = 0
        nSo_luong = frmMain.tblDetail(frmMain.grdDetail.CurrentRowIndex).Item("so_luong")
        frmMain.tblDetail(frmMain.grdDetail.CurrentRowIndex).Item("tong_mm") = CType(txtTongmm.Text, Decimal) / 1000 * nSo_luong
        frmMain.grdDetail.Refresh()
        frmMain.UpdateList()
        Me.Close()
    End Sub
End Class